﻿
#region Copyright (c) 2011 R. Uittenbosch
/*
* Copyright (C) 2011 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;



namespace DatabaseViewer
{
    public class ViewConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("Views", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(ViewsCollection),
            AddItemName = "add",
            ClearItemsName = "clear",
            RemoveItemName = "remove")]

        public ViewsCollection Views
        {
            get
            {
                return (ViewsCollection)base["Views"];
            }                
        }

        [ConfigurationProperty("Title", DefaultValue = "", IsRequired = true, IsKey = true)]
        public string Title
        {
            get
            {
                return (string)this["Title"];
            }
            set
            {
                this["Title"] = value;
            }
        }

        [ConfigurationProperty("Default", DefaultValue = "", IsRequired = true, IsKey = true)]
        public string Default
        {
            get
            {
                return (string)this["Default"];
            }
            set
            {
                this["Default"] = value;
            }
        }
    }

    public class ViewsCollection : ConfigurationElementCollection
    {
        public ViewsCollection()
        {
            ViewConfigElement item = (ViewConfigElement)CreateNewElement();
            Add(item);
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }


        protected override ConfigurationElement CreateNewElement()
        {
            return new ViewConfigElement();
        }

        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((ViewConfigElement)element).Name;
        }

        public ViewConfigElement this[int index]
        {
            get
            {
                return (ViewConfigElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        public int IndexOf(ViewConfigElement view)
        {
            return BaseIndexOf(view);
        }

        public void Add(ViewConfigElement view)
        {
            BaseAdd(view);
        }
        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
        }

        public void Remove(ViewConfigElement view)
        {
            if (BaseIndexOf(view) >= 0)
                BaseRemove(view.Name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Clear()
        {
            BaseClear();
        }
    }

    public class ViewConfigElement : ConfigurationElement
    {
        public ViewConfigElement()
        {
        }

        public ViewConfigElement(string name, Type type, string connectionstring)
        {
            this.Name = name;
            this.Type = type;
            this.ConnectionString = connectionstring;

        }

        [ConfigurationProperty("name", DefaultValue = "", IsRequired = true, IsKey = true)]
        public string Name
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("connectionstring", IsRequired = true)]
        public string ConnectionString
        {
            get
            {
                return (string)this["connectionstring"];
            }
            set
            {
                this["connectionstring"] = value;
            }
        }     

        [ConfigurationProperty("type", IsRequired = true)]
        [TypeConverter(typeof(TypeNameConverter))]
        public Type Type
        {
            get
            {
                return (Type)this["type"];
            }
            set
            {
                this["type"] = value;
            }
        }     
    }   
}
